﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BE;
using BLL;
using System.Security;


namespace UI
{
    public partial class AddForm : System.Web.UI.Page
    {
        List<Vertex> vertices;
        string selected;
        
        protected void Page_Load(object sender, EventArgs e)
        {
			selected = Request.QueryString["action"];
            setInvisible();
			
            getSelectedselected();
			if ( !IsPostBack )
			{
				MakeDropDownList();
			}
        }

        private void setInvisible()
        {
            lblLocation.Visible = false;
            lblWeight.Visible = false;
            lblStraight.Visible = false;
            lblLocation1.Visible = false;
            lblLocation2.Visible = false;
            lblDiretions.Visible = false;

            ddlLocation.Visible = false;
            txtWeight.Visible = false;
            chkStraight.Visible = false;
            ddlLocation1.Visible = false;
            ddlLocation2.Visible = false;
            txtADirections.Visible = false;
        }

        private void MakeDropDownList()
        {
            ddlLocation.Items.Clear();
            ddlLocation1.Items.Clear();
            ddlLocation2.Items.Clear();

            VertexWorker vW = new VertexWorker();
            
            ddlLocation.Items.Clear();
            ddlLocation1.Items.Clear();
            ddlLocation2.Items.Clear();

            vertices = vW.GetAll();
            
            foreach(Vertex v in vertices)
            {
                ddlLocation.Items.Add(new ListItem(v.Name,""+v.ID));
				ddlLocation1.Items.Add( new ListItem( v.Name, "" + v.ID ) );
				ddlLocation2.Items.Add( new ListItem( v.Name, "" + v.ID ) );
            }
        }

        private void getSelectedselected()
        {
            if (selected == "Room")
            {
                lblAddTitle.Text = "Add Room";
                lblLocation.Visible = true;
                ddlLocation.Visible = true;
            }
            else if (selected == "Location")
            {
                lblAddTitle.Text = "Add Location";
            }
            else if (selected == "Walkway")
            {
                lblAddTitle.Text = "Add Walkway";
                lblWeight.Visible = true;
                lblStraight.Visible = true;
                lblLocation1.Visible = true;
                lblLocation2.Visible = true;
                lblDiretions.Visible = true;

                txtWeight.Visible = true;
                chkStraight.Visible = true;
                ddlLocation1.Visible = true;
                ddlLocation2.Visible = true;
                txtADirections.Visible = true;

            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (selected == "Room")
                addRoom();

            else if (selected == "Location")
                addLocation();

            else if (selected == "Walkway")
                addWalkway();
        }

        private void addWalkway()
        {
            int weight;
            EdgeWorker eW = new EdgeWorker();
            Edge ed = new Edge();

            bool result = Int32.TryParse(txtWeight.Text, out weight);
            if (!result)
            {
                lblDone.Text = "Weight needs to be a number.";
                return;
            }

            String username = User.Identity.Name.ToString();
            
            ed.Name = txtName.Text;
            ed.Direction = txtADirections.Text;
            ed.Weight = weight;
            ed.Straight = chkStraight.Checked;
            ed.L1 = Convert.ToInt32(ddlLocation1.SelectedValue);
			ed.L2 = Convert.ToInt32( ddlLocation2.SelectedValue );
            eW.Add(ed, username);

            lblDone.Text = "Walkway "+ed.Name+" has been added";
        }

        private void addLocation()
        {
            VertexWorker vW = new VertexWorker();
            Vertex v = new Vertex();

            String username = User.Identity.Name.ToString();

            v.Name = txtName.Text;
            vW.Add(v, username);

            lblDone.Text = "Location "+v.Name+" has been added";
        }

        private void addRoom()
        {
            RoomWorker rW = new RoomWorker();
            Room r = new Room();

            String username = User.Identity.Name.ToString();

            r.Name = txtName.Text;
			if ( ddlLocation.Items.Count > 0 )
			{
				r.LocationID = Convert.ToInt32( ddlLocation.SelectedValue );
			}
			else
				r.LocationID = 0;
            rW.Add(r, username);

            lblDone.Text = "Room "+r.Name+" has been added";
        }
    }
}